package com.zuzhili.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.dispatcher.R;

public class CustomDlg extends Dialog {
	protected Activity m_context;
	protected int m_iPosX;
	protected int m_iPosY;
	ListView msinglechoiceLayout;
	private Window window = null;
	OnClickListener msingleChoiceListener;
	View mContentView;
	InputMethodManager imm;

	public CustomDlg(Activity context, int style) {
		super(context, style);
		m_context = context;
	}

	@Override
	public void dismiss() {
		// TODO Auto-generated method stub
		if(imm!=null){
			
			imm.hideSoftInputFromWindow(mContentView.getWindowToken(), 0);
		}
		super.dismiss();
	}

	public void setList(String[] items, OnClickListener listener) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		msinglechoiceLayout = new ListView(m_context);
		addContentView(msinglechoiceLayout, new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		msinglechoiceLayout.setCacheColorHint(Color.argb(0, 0, 0, 0));// 把listView的缓存色为透明
		// 数组适配器
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(m_context,
				R.layout.simple_list_item, items);

		msingleChoiceListener = listener;
		msinglechoiceLayout.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				msingleChoiceListener.onClick(CustomDlg.this, position);
			}
		});
		msinglechoiceLayout.setAdapter(adapter);// 拿来填充
		setProperty();
		// Drawable draw =
		// m_context.getResources().getDrawable(R.drawable.frame_img);
		// msinglechoiceLayout.setBackgroundDrawable(draw);
		// draw = m_context.getResources().getDrawable(R.drawable.table_line);
		// msinglechoiceLayout.setDivider(draw);
		// msinglechoiceLayout.setVerticalScrollBarEnabled(false);
	}

	public void setSingleChoice(String[] items, int select,
			OnClickListener listener) {
		// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		//
		//
		// msinglechoiceLayout = new ListView(m_context);
		//
		// msinglechoiceLayout.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		// addContentView(msinglechoiceLayout, new
		// LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		// msinglechoiceLayout.setCacheColorHint(Color.argb(0, 0, 0, 0));
		// msinglechoiceLayout.setVerticalScrollBarEnabled(false);
		//
		// msingleChoiceListener = listener;
		//
		// ArrayAdapter<String> adapter = new
		// ArrayAdapter<String>(m_context,R.layout.simple_list_item_single_choice,items);
		//
		// msinglechoiceLayout.setAdapter(adapter);
		//
		// msinglechoiceLayout.setOnItemClickListener(new OnItemClickListener()
		// {
		// public void onItemClick(AdapterView<?> parent, View view, int
		// position, long id){
		// msingleChoiceListener.onClick(CustomDlg.this, position);
		// }
		//
		// });
		//
		// msinglechoiceLayout.setItemChecked(0, true);
		// Drawable draw =
		// m_context.getResources().getDrawable(R.drawable.frame_img);
		// msinglechoiceLayout.setBackgroundDrawable(draw);
		// draw = m_context.getResources().getDrawable(R.drawable.table_line);
		// msinglechoiceLayout.setDivider(draw);
		//
		// setProperty();

	}

	public void setDisplayView(View view, InputMethodManager imm) {
		if (imm != null) {

			this.imm = imm;
		}
		mContentView = view;
		setContentView(view);// 设置对话框的布局
		setProperty();
	}

	public void setDisplayView(String title, String msg, String LBtn,
			String RBtn) {
		LayoutInflater factory = LayoutInflater.from(m_context);
		final View v = factory.inflate(R.layout.confirm_dialog, null);
		mContentView = v;
		// TextView tvtitle=(TextView)v.findViewById(R.id.dialog_title);
		// if(title!=null){
		// tvtitle.setText(title);
		// }else{
		// tvtitle.setText("提醒");
		// }
		TextView tvmsg = (TextView) v.findViewById(R.id.content);
		if (msg != null) {
			tvmsg.setText(msg);
		} else {
			tvmsg.setVisibility(View.GONE);
		}
		Button left = (Button) v.findViewById(R.id.ok);
		if (LBtn != null) {
			left.setText(LBtn);
		} else {
			left.setVisibility(View.GONE);
		}
		Button right = (Button) v.findViewById(R.id.cancel);
		if (RBtn != null) {
			right.setText(RBtn);
		} else {
			right.setVisibility(View.GONE);
		}
		setContentView(v);// 设置对话框的布局

		setProperty();
	}

	public void setDisplayWithView(int resource, String title, String msg,
			String LBtn, String RBtn) {
		// LayoutInflater factory = LayoutInflater.from(m_context);
		// final View v = factory.inflate(resource, null);
		// mContentView=v;
		// TextView tvtitle=(TextView)v.findViewById(R.id.dialog_title);
		// if(title!=null){
		// tvtitle.setText(title);
		// }else{
		// tvtitle.setVisibility(View.GONE);
		// }
		// TextView tvmsg=(TextView)v.findViewById(R.id.dialog_msg);
		// if(msg!=null){
		// tvmsg.setText(msg);
		// }else{
		// tvmsg.setVisibility(View.GONE);
		// }
		// Button left = (Button)v.findViewById(R.id.dialog_btn_Left);
		// if(LBtn!=null){
		// left.setText(LBtn);
		// }else{
		// left.setVisibility(View.GONE);
		// }
		// Button right = (Button)v.findViewById(R.id.dialog_btn_Right);
		// if(RBtn!=null){
		// right.setText(RBtn);
		// }else{
		// right.setVisibility(View.GONE);
		// }
		// setContentView(v);//设置对话框的布局
		//
		//
		// setProperty();
	}

	public void setLBtnListner(View.OnClickListener listener) {
		if (mContentView != null) {
			Button left = (Button) mContentView.findViewById(R.id.ok);
			left.setOnClickListener(listener);
		}
	}

	public void setRBtnListner(View.OnClickListener listener) {
		if (mContentView != null) {
			Button right = (Button) mContentView.findViewById(R.id.cancel);
			right.setOnClickListener(listener);
		}
	}

	// 要显示这个对话框，只要创建该类对象．然后调用该函数即可．
	public void setProperty() {
		window = getWindow();// 　　　得到对话框的窗口�
		WindowManager.LayoutParams wl = window.getAttributes();
		WindowManager m = m_context.getWindowManager();
		Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
		// wl.height = (int) (d.getHeight() * 0.8); //高度设置为屏幕的0.6
		wl.width = (int) (d.getWidth() * 0.95); // 宽度设置为屏幕的0.95
		// wl.alpha=0.8f;
		// window.setAttributes(wl);
	}

	public void setPropertyTop() {
		window = getWindow();// 　　　得到对话框的窗口�
		WindowManager.LayoutParams wl = window.getAttributes();
		WindowManager m = m_context.getWindowManager();
		Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
		DisplayMetrics dMetrics = new DisplayMetrics();
		d.getMetrics(dMetrics);
		wl.y = (int) (100 * dMetrics.density);
		window.setAttributes(wl);
	}

	public void setProperty(float height) {
		window = getWindow();// 　　　
		WindowManager.LayoutParams wl = window.getAttributes();
		WindowManager m = m_context.getWindowManager();
		Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
		// wl.height = (int) (d.getHeight() * height); //高度设置为屏幕的0.6
		// wl.width = (int) (d.getWidth() * 1); //宽度设置为屏幕的0.95
		wl.alpha = 0.5f;
		window.setAttributes(wl);
	}

	public void setPropertyTop(int x, int y, double centi) {
		window = getWindow();// 　　　
		WindowManager.LayoutParams wl = window.getAttributes();
		WindowManager m = m_context.getWindowManager();
		Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
		wl.width = (int) (d.getWidth() * centi); // 宽度设置为屏幕的0.95
		wl.x = x;
		wl.y = y;
		wl.gravity = Gravity.TOP;
		window.setAttributes(wl);
	}

}
